package edu.bbu.cshu.wmpc.loggin;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
/**
 * <ul>
 * <li><b><i>EventLogger</i></b></li>
 * <li style="list-style: none;"></li>
 * <li style="list-style: none;">
 * Enumeration used for writing events into a log file.
 * 
 * @author WMPC team
 * </li>
 * </ul>
 */ 
public enum EventLogger {
	/**
	 * <ul>
	 * <li><b><i>INSTANCE</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * Ensures that only a single instance of the enumeration exists.
	 * 
	 * </li>
	 * </ul>
	 */
	INSTANCE;
	/**
	 * <ul>
	 * <li><b><i>logger</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * {@link Logger} used for logging events.
	 * 
	 * </li>
	 * </ul>
	 */
	private Logger logger = null;
	/**
	 * <ul style="margin-top: 24px;">
	 * <li><b><i>configureLogger</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * <p style="margin-left: 7px;">{@code public void configureLogger(}{@link String} {@code URL)}</p>
	 * <br>	
	 * Sets the ULR of the properties file used by the {@code EventLogger}.
	 * 
	 * @param URL - {@code String} specifying the location of the log file
	 * </li>
	 * </ul>
	 */
	public void configureLogger(String URL) {
		File file = new File(URL);
		if (file.exists()) {
			if (isOK(URL)) {
				try {
					logger = Logger.getLogger(EventLogger.class.getName());
					PropertyConfigurator.configure(URL);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
	}
	/**
	 * <ul style="margin-top: 24px;">
	 * <li><b><i>writeToLog</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * <p style="margin-left: 7px;">{@code public void writeToLog(}{@link Level} {@code lvl,} {@link String} {@code msg)}</p>
	 * <br>	
	 * Writes an entry in the log file, taking into account the specified {@code level}. 
	 * 
	 * @param lvl - {@code Level} of the massage 
	 * @param msg - message to be logged
	 * </li>
	 * </ul>
	 */
	public void writeToLog(Level lvl, String msg) {
		if (logger != null) {
			logger.log(lvl, msg);
		}
	}
	/**
	 * <ul style="margin-top: 24px;">
	 * <li><b><i>isOK</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * <p style="margin-left: 7px;">{@code private boolean isOK(}{@link String} {@code URL)}</p>
	 * <br>	
	 * Determines is the file to be logged in, specified in the properties file, exists.
	 * 
	 * @param URL - {@code String} specifying the location of the properties file
	 * @return if the test is positive or not
	 * </li>
	 * </ul>
	 */
	@SuppressWarnings("finally")
	private boolean isOK(String URL) {
		boolean ok = false;
		try {
	        File file = new File(URL);
	        BufferedReader reader = new BufferedReader(new FileReader(file));
	        String line = "";
	        File check;
	        while((line = reader.readLine()) != null) {
	        	if (line.matches("log4j.appender.FILE.File=.*")) {
	        		line = line.replaceAll("(log4j.appender.FILE.File=)(.*)", "$2");

	        		check = new File(line);
	        		if (check.isFile()) {
	        			ok = true;;    			
	        		}
	        	}
	        }
	        reader.close();
	    }
	    catch (Exception ex) {
	        ex.printStackTrace();
	    } finally {
	    	return ok;
	    }
	}
}
